package com.caigou.modules.forum.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.bean.PostBean
import com.caigou.modules.forum.repository.ForumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/17
 */
@HiltViewModel
class MarketViewModel @Inject constructor(private val repository: ForumRepository) : ViewModel() {

    val marketListLiveData = MutableLiveData<Resource<PostBean>>()

    //    val postLikeLiveData = MutableLiveData<Resource<String>>()
    val historyPostListLiveData = MutableLiveData<Resource<PostBean>>()

    fun getMarketList(page: Int, limit: Int, superMaterialType: Int, currentUserId: String) {
        marketListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMarketList(page, limit, superMaterialType, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    marketListLiveData.value = it
                }
        }
    }


    fun getMarketHistoryList(page: Int, limit: Int, userId: String) {
        historyPostListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getHistoryPostList(page, limit, 6, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    historyPostListLiveData.value = it
                }
        }
    }

//    fun setPostLike(dataId: Int, senderId: String, receiverId: String) {
//        postLikeLiveData.value = Resource.Loading()
//        viewModelScope.launch {
//            repository.setPostLike(dataId, senderId, receiverId)
//                .map {
//                    if (it.code == 0) {
//                        Resource.Success(it.msg)
//                    } else {
//                        Resource.DataError(it.code, it.msg)
//                    }
//                }
//                .catch {
//                    val message = if (TextUtils.isEmpty(it.message)) {
//                        ""
//                    } else {
//                        it.message!!
//                    }
//                    Resource.DataError<String>(-1, message)
//                    Logger.e("catch: ${it.message}")
//                }
//                .collectLatest {
//                    postLikeLiveData.value = it
//                }
//        }
//    }

}